package yxy.game.pm2.command.union;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.union.UnionHandle;

/** 联盟 联盟搜索 */
@MessageType(proto = Command.UnionSearch, classtype = UnionMessage.UnionSearchRequest.class)
public class CMD_UnionSearch extends Command<UnionMessage.UnionSearchRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionSearchRequest request) {
		String playerCode = messageSource.getTag();
		String keyword = request.getKeyword();

		UnionMessage.UnionSearchResponse.Builder response = UnionMessage.UnionSearchResponse.newBuilder();
		try {
			PlayerBean playerBean = PlayerBean.get(playerCode);
			Result result = new UnionHandle().search(playerBean.getServerId(), keyword, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionSearchResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}